package ch.fhnw.qualiscan.application.controllers;

import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionnaireModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.AdminPanelView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.helpers.AdminParticipantTableArgs;
import ch.fhnw.qualiscan.core.db.helpers.AdminQuestionnairesTableArgs;
import ch.fhnw.qualiscan.core.db.helpers.adminRoleTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Questionnaire;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.University;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.libraries.LanguageLibrary;
import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItemContainer;
import java.util.List;

/**
 * This class handles all logic in context with the AdminPanel. The corresponding view is AdminPanelView in the views package.
 * @author bengro
 */
public class AdminPanel extends Controller {

    private UserModel userModel;
    private SurveyModel surveyModel;
    private DomainModel domainModel;
    private AdminModel adminModel;    
    private QuestionnaireModel qModel;
    
    @Override
    public void init() {
        
    }

    @Override
    public void setView() {
        view = new AdminPanelView(this);
        view.buildLayout();
    }

    @Override
    public void loadModels() {
        userModel = new UserModel();
        surveyModel = new SurveyModel();
        domainModel = new DomainModel();
        adminModel = new AdminModel();
        qModel = new QuestionnaireModel();
    }

    @Override
    public void setNeededPermissions() {
        neededPermissions.add(Permissions.LOGGEDIN);
        neededPermissions.add(Permissions.ADMINISTRATOR);
    }

    @Override
    public void urlFragmentChanged(int fragmentId, String fragment) {
        
    }
    
    public Container getQuestionaireData() {
        final BeanItemContainer<AdminQuestionnairesTableArgs> tableContainer;
        tableContainer = new BeanItemContainer<AdminQuestionnairesTableArgs>(AdminQuestionnairesTableArgs.class);
        
        List<Questionnaire> questionnaires = qModel.getQuestionnaires();
        
        for(Questionnaire questionnaire : questionnaires) {
            AdminQuestionnairesTableArgs qta = new AdminQuestionnairesTableArgs(questionnaire, questionnaire.getTitle(), questionnaire.getDescription(), questionnaire.getCreated().toString());
            tableContainer.addBean(qta);
        }
        
        return tableContainer;
    }
    
    
    /**
     * loads the data for the user table.
     * @return 
     */
    public BeanItemContainer<AdminParticipantTableArgs> getUserTableData() {
        // data
        final BeanItemContainer<AdminParticipantTableArgs> tableContainer;
        tableContainer = new BeanItemContainer<AdminParticipantTableArgs>(AdminParticipantTableArgs.class);

        List<User> users = userModel.getUsers();

        for(User o : users) {
            String role = userModel.getRoleText(o.getRole(), userSession.getLanguage()).getTitle();
            String language = "";
            if (o.getLanguage() != null) {
                language = o.getLanguage().getLocale();
            }
            String date = o.getRegdate().toString();

            AdminParticipantTableArgs pta = new AdminParticipantTableArgs(o, o.getFirstname(), o.getLastname(), o.getEmail(), role, language, date);
            tableContainer.addBean(pta);
        }

        return tableContainer;
    }
    
    /**
     * bean container for list of all surveys.
     * @return 
     */
    public BeanItemContainer<Survey> getSurveyTableData() {
        
        final BeanItemContainer<Survey> tableContainer;
        tableContainer = new BeanItemContainer<Survey>(Survey.class);
		
        List<Survey> surveyList = getAdminModel().getSurveys();
        for(int i = 0; i < surveyList.size(); i++) {
                tableContainer.addBean(surveyList.get(i));
        }
        
        return tableContainer;
    }

    /**
     * Returns table data used for displaying all universities.
     * @return 
     */
    public BeanItemContainer<University> getUniversitiesTableData() {
        BeanItemContainer<University> tableContainer = new BeanItemContainer<University>(University.class);
		
        List<University> universityList = getAdminModel().getUniversities();
        for(int i = 0; i < universityList.size(); i++) {
                tableContainer.addBean(universityList.get(i));
        }
        
        return tableContainer;
    }
    
    /**
     * Returns the table data used for displaying all system roles.
     * @return 
     */
    public BeanItemContainer<adminRoleTableArgs> getRolesTableData() {
        BeanItemContainer<adminRoleTableArgs> tableContainer = new BeanItemContainer<adminRoleTableArgs>(adminRoleTableArgs.class);
	
        List<Role> roleList = getAdminModel().getRoles();
	for(int i = 0; i < roleList.size(); i++) {
            adminRoleTableArgs arta = new adminRoleTableArgs();
            arta.setRole(roleList.get(i));
            arta.setPermission(roleList.get(i).getPermissions().getTitle());
            arta.setTitleEng(getAdminModel().getRoleText(roleList.get(i), new LanguageLibrary().getDefaultLanguage()).getTitle());
		
            tableContainer.addBean(arta);
	}
        
        return tableContainer;
    }
    
    public void loadQuestionnairePage(Questionnaire questionnaire) {
        super.getAppData().getLoaderLib().setUrl("AdminQuestionnaire/0/" + questionnaire.getId());
    }
    
    /**
     * @return the surveyModel
     */
    public SurveyModel getSurveyModel() {
        return surveyModel;
    }

    /**
     * @return the domainModel
     */
    public DomainModel getDomainModel() {
        return domainModel;
    }

    /**
     * @return the adminModel
     */
    public AdminModel getAdminModel() {
        return adminModel;
    }

    /**
     * @return the qModel
     */
    public QuestionnaireModel getqModel() {
        return qModel;
    }

   
    

}
